package com.lagou.homework_1_3;
//定义Map集合
import java.util.*;

public class MapTest {

    public static void main(String[] args) {

        //new Map类型的对象
        Map<Student,String> st1 = new HashMap<>();

        //new Student类型的对象
        Student s1 = new Student("赵",12);
        Student s2 = new Student("王",20);
        Student s3 = new Student("黄",24);
        Student s4 = new Student("韩",28);
        Student s5 = new Student("钟",17);

        //用Student对象作为key，用字符串(此表示表示学生的住址)作为value 初始化Map
        st1.put(s1,"上海市静安区");
        st1.put(s2,"上海市浦东新区区");
        st1.put(s3,"上海市松江区");
        st1.put(s4,"上海市闵行区");
        st1.put(s5,"上海市虹口区");

        //利用四种方式遍历Map集合中的内容，格式：key::value

        //第一种
        for (Student s7:st1.keySet()){
            System.out.println(s7+"::"+st1.get(s7));
        }

        System.out.println("-------------------------------------------------");

        //第二种
        Set<Map.Entry<Student, String>> entries = st1.entrySet();
        for (Map.Entry<Student, String> m1:entries) {
            System.out.println(m1.getKey()+"::"+ m1.getValue());
        }

        System.out.println("-------------------------------------------------");

        //第三种方法
        Iterator<Map.Entry<Student, String>> entries1 = st1.entrySet().iterator();
        while (entries1.hasNext()){
            Map.Entry<Student, String> next = entries1.next();
            System.out.println(next.getKey()+"::"+next.getValue());
        }

        System.out.println("-------------------------------------------------");

        //第四种方法
        st1.forEach((key,value)-> {
            System.out.println( key + "::" + value);
        });


    }
}
